home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / src / shell.c.orig < prev    next >
Encoding:
Text File  |  1991-09-12  |  35.1 KB  |  1,463 lines

  1. /* shell.c -- GNU's idea of the POSIX shell specification.
  2.  
  3.    This file is part of Bash, the Bourne Again SHell.  Bash is free
  4.    software; no one can prevent you from reading the source code, or
  5.    giving it to someone else.  This file is copyrighted under the GNU
  6.    General Public License, which can be found in the file called
  7.    COPYING.
  8.  
  9.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  10.  
  11.    This file is part of GNU Bash.
  12.  
  13.    Bash is distributed in the hope that it will be useful, but WITHOUT
  14.    ANY WARRANTY.  No author or distributor accepts responsibility to
  15.    anyone for the consequences of using it or for whether it serves
  16.    any particular purpose or works at all, unless he says so in
  17.    writing.  Refer to the GNU Emacs General Public License for full
  18.    details.
  19.  
  20.    Everyone is granted permission to copy, modify and redistribute
  21.    Bash, but only under the conditions described in the GNU General
  22.    Public License.  A copy of this license is supposed to have been
  23.    given to you along with GNU Emacs so you can know your rights and
  24.    responsibilities.  It should be in a file named COPYING.
  25.  
  26.    Among other things, the copyright notice and this notice must be
  27.    preserved on all copies.
  28.  
  29.   Birthdate:
  30.   Sunday, January 10th, 1988.
  31.   Initial author: Brian Fox
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <signal.h>
  36. #include <errno.h>
  37. #include <sys/types.h>
  38. #if !defined (sony)
  39. #include <fcntl.h>
  40. #endif /* sony */
  41. #include <sys/file.h>
  42. #include "posixstat.h"
  43. #include <pwd.h>
  44.  
  45. #if defined (HAVE_VPRINTF)
  46. #include <varargs.h>
  47. #endif
  48.  
  49. #include "shell.h"
  50. #include "flags.h"
  51.  
  52. #if defined (JOB_CONTROL)
  53. #include "jobs.h"
  54. #endif /* JOB_CONTROL */
  55.  
  56. #if defined (USG) && !defined (isc386)
  57. struct passwd *getpwuid ();
  58. #endif
  59.  
  60. extern char *dist_version;
  61. extern int build_version;
  62.  
  63. extern int yydebug;
  64.  
  65. /* Non-zero means that this shell has already been run; i.e. you should
  66.    call shell_reinitialize () if you need to start afresh. */
  67. int shell_initialized = 0;
  68.  
  69. /* The current maintainer of the shell.  You change this in the
  70.    Makefile. */
  71. #if !defined (MAINTAINER)
  72. #define MAINTAINER "deliberately-anonymous"
  73. #endif
  74.  
  75. char *the_current_maintainer = MAINTAINER;
  76.  
  77. #ifndef PPROMPT
  78. #define PPROMPT "bash\\$ "
  79. #endif
  80. char *primary_prompt = PPROMPT;
  81.  
  82. #ifndef SPROMPT
  83. #define SPROMPT "bash> "
  84. #endif
  85. char *secondary_prompt = SPROMPT;
  86.  
  87. COMMAND *global_command = (COMMAND *)NULL;
  88.  
  89. /* Non-zero after SIGINT. */
  90. int interrupt_state = 0;
  91.  
  92. /* The current user's name. */
  93. char *current_user_name = (char *)NULL;
  94.  
  95. /* The current host's name. */
  96. char *current_host_name = (char *)NULL;
  97.  
  98. /* Non-zero means that this shell is a login shell.
  99.    Specifically:
  100.    0 = not login shell.
  101.    1 = login shell from getty (or equivalent fake out)
  102.   -1 = login shell from "-login" flag.
  103.   -2 = both from getty, and from flag.
  104.  */
  105. int login_shell = 0;
  106.  
  107. /* Non-zero means that at this moment, the shell is interactive. */
  108. int interactive = 0;
  109.  
  110. /* Non-zero means that the shell was started as an interactive shell. */
  111. int interactive_shell = 1;
  112.  
  113. /* Non-zero means to remember lines typed to the shell on the history
  114.    list.  This is different than the user-controlled behaviour; this
  115.    becomes zero when we read lines from a file, for example. */
  116. int remember_on_history = 1;
  117.  
  118. /* Non-zero means this shell is restricted. */
  119. int restricted = 0;
  120.  
  121. /* Special debugging helper. */
  122. int debugging_login_shell = 0;
  123.  
  124. /* The environment that the shell passes to other commands. */
  125. char **shell_environment;
  126.  
  127. /* Non-zero when we are executing a top-level command. */
  128. int executing = 0;
  129.  
  130. /* The number of commands executed so far. */
  131. int current_command_number = 1;
  132.  
  133. /* The environment at the top-level REP loop.  We use this in the case of
  134.    error return. */
  135. jmp_buf top_level, catch;
  136.  
  137. /* Non-zero is the recursion depth for commands. */
  138. int indirection_level = 0;
  139.  
  140. /* The number of times BASH has been executed.  This is set
  141.    by initialize_variables () in variables.c. */
  142. int shell_level = 0;
  143.  
  144. /* The name of this shell, as taken from argv[0]. */
  145. char *shell_name = (char *)NULL;
  146.  
  147. /* time in seconds when the shell was started */
  148. time_t shell_start_time;
  149.  
  150. /* The name of the .(shell)rc file. */
  151. char *bashrc_file = "~/.bashrc";
  152.  
  153. /* Non-zero means to act more like the Bourne shell on startup. */
  154. int act_like_sh = 0;
  155.  
  156. /* Values for the long-winded argument names. */
  157. int debugging = 0;        /* Do debugging things. */
  158. int no_rc = 0;            /* Don't execute ~/.bashrc */
  159. int no_profile = 0;        /* Don't execute .profile */
  160. int do_version = 0;        /* Display interesting version info. */
  161. int quiet = 0;            /* Be quiet when starting up. */
  162. int make_login_shell = 0;    /* Make this shell be a `-bash' shell. */
  163. int no_line_editing = 0;    /* Don't do fancy line editing. */
  164. int no_brace_expansion = 0;    /* Non-zero means no foo{a,b} -> fooa fooa. */
  165.  
  166. /* Some long-winded argument names.  These are obviously new. */
  167. #define Int 1
  168. #define Charp 2
  169. struct {
  170.   char *name;
  171.   int *value;
  172.   int type;
  173. } long_args[] = {
  174.   { "debug", &debugging, Int },
  175.   { "norc", &no_rc, Int },
  176.   { "noprofile", &no_profile, Int },
  177.   { "rcfile", (int *)&bashrc_file, Charp},
  178.   { "version", &do_version, Int},
  179.   { "quiet", &quiet, Int},
  180.   { "login", &make_login_shell, Int},
  181.   { "nolineediting", &no_line_editing, Int},
  182.   { "nobraceexpansion", &no_brace_expansion, Int},
  183.   { (char *)NULL, (int *)0x0, 0 }
  184. };
  185.  
  186. /* The number of lines that Bash has added to this history session. */
  187. int history_lines_this_session = 0;
  188.  
  189. /* The number of lines that Bash has read from the history file. */
  190. int history_lines_in_file = 0;
  191.  
  192. /* These are extern so execute_simple_command can set them, and then
  193.    longjmp back to main to execute a shell script, instead of calling
  194.    main () again and resulting in indefinite, possibly fatal, stack
  195.    growth. */
  196. jmp_buf subshell_top_level;
  197. int subshell_argc;
  198. char **subshell_argv;
  199. char **subshell_envp;
  200.  
  201. main (argc, argv, env)
  202.      int argc;
  203.      char **argv, **env;
  204. {
  205.   extern int last_command_exit_value;
  206.   extern char *base_pathname ();
  207.   int i, arg_index, locally_skip_execution;
  208.   int top_level_arg_index, read_from_stdin;
  209.   FILE *default_input;
  210.   char *local_pending_command = (char *)NULL;
  211. #if defined (JOB_CONTROL)
  212.   extern int job_control;
  213. #endif /* JOB_CONTROL */
  214.  
  215. #if defined (AUX)
  216. #include <compat.h>
  217.   set42sig ();
  218.   setcompat (getcompat() | COMPAT_BSDGROUPS | COMPAT_BSDSIGNALS |
  219.          COMPAT_BSDTTY | COMPAT_EXEC | COMPAT_SYSCALLS);
  220. #endif /* AUX */
  221.  
  222.   /* Wait forever if we are debugging a login shell. */
  223.   while (debugging_login_shell);
  224.  
  225.   if (setjmp (subshell_top_level))
  226.     {
  227.       argc = subshell_argc;
  228.       argv = subshell_argv;
  229.       env = subshell_envp;
  230.     }
  231.  
  232.   /* Initialize local variables for all `invocations' of main (). */
  233.   arg_index = 1;
  234.   local_pending_command = (char *)NULL;
  235.   locally_skip_execution = 0;
  236.   read_from_stdin = 0;
  237.   default_input = stdin;
  238.  
  239.   /* Fix for the `infinite process creation' bug when running shell scripts
  240.      from startup files on System V. */
  241.   login_shell = make_login_shell = 0;
  242.  
  243.   /* If this shell has already been run, then reinitialize it to a
  244.      vanilla state. */
  245.   if (shell_initialized || shell_name)
  246.     {
  247.       /* Make sure that we do not infinitely recurse as a login shell. */
  248.       if (*shell_name == '-')
  249.     shell_name++;
  250.  
  251.       shell_reinitialize ();
  252.       if (setjmp (top_level))
  253.     exit (2);
  254.     }
  255.  
  256.   /* Here's a hack.  If the name of this shell is "sh", then don't do
  257.      any startup files; just try to be more like /bin/sh. */
  258.   {
  259.     char *tshell_name = base_pathname (argv[0]);
  260.  
  261.     if (*tshell_name == '-')
  262.       tshell_name++;
  263.  
  264.     if (strcmp (tshell_name, "sh") == 0)
  265.       act_like_sh++;
  266.   }
  267.  
  268.   yydebug = 0;
  269.  
  270.   shell_environment = env;
  271.   shell_name = argv[0];
  272.   dollar_vars[0] = savestring (shell_name);
  273.  
  274.   if (*shell_name == '-')
  275.     {
  276.       shell_name++;
  277.       login_shell++;
  278.     }
  279.  
  280. #ifdef JOB_CONTROL
  281.   if (act_like_sh)
  282.     job_control = 0;
  283. #endif
  284.  
  285.   shell_start_time = NOW;    /* NOW now defined in general.h */
  286.  
  287.   /* A program may start an interactive shell with
  288.      "execl ("/bin/bash", "-", NULL)".  If so, default the name of this
  289.      shell to our name. */
  290.   if (!shell_name || !*shell_name || (strcmp (shell_name, "-") == 0))
  291.     shell_name = "bash";
  292.      
  293.   /* Parse argument flags from the input line. */
  294.  
  295.   /* Find full word arguments first. */
  296.   while ((arg_index != argc) && *(argv[arg_index]) == '-')
  297.     {
  298.       for (i = 0; long_args[i].name; i++)
  299.     {
  300.       if (strcmp (&(argv[arg_index][1]), long_args[i].name) == 0)
  301.         {
  302.           if (long_args[i].type == Int)
  303.         *(long_args[i].value) = 1;
  304.           else
  305.         {
  306.           if (!argv[++arg_index])
  307.             {
  308.               report_error ("%s: Flag `%s' expected an argument",
  309.                     shell_name, long_args[i].name);
  310.               exit (1);
  311.             }
  312.           else
  313.             *long_args[i].value = (int)argv[arg_index];
  314.         }
  315.           goto next_arg;
  316.         }
  317.     }
  318.       break;            /* No such argument.  Maybe flag arg. */
  319.     next_arg:
  320.       arg_index++;
  321.     }
  322.  
  323.   /* If user supplied the "-login" flag, then set and invert LOGIN_SHELL. */
  324.   if (make_login_shell)
  325.     login_shell = -++login_shell;
  326.  
  327.   /* All done with full word args; do standard shell arg parsing.*/
  328.   while (arg_index != argc && argv[arg_index] &&
  329.      (*(argv[arg_index]) == '-' || (*argv[arg_index] == '+')))
  330.     {
  331.       /* There are flag arguments, so parse them. */
  332.       int arg_character;
  333.       int on_or_off = (*argv[arg_index]);
  334.       int  i = 1;
  335.       char *o_option;
  336.       int next_arg = arg_index + 1;
  337.  
  338.       /* A single `-' signals the end of options.  From the 4.3 BSD sh.
  339.      An option `--' means the same thing; this is the standard
  340.      getopt () meaning. */
  341.       if (((argv[arg_index][0] == '-') && (argv[arg_index][1] == '\0')) ||
  342.       (strcmp (argv[arg_index], "--") == 0))
  343.     {
  344.       arg_index++;
  345.       goto after_flags;
  346.     }
  347.  
  348.       while (arg_character = (argv[arg_index])[i++])
  349.     {
  350.       switch (arg_character)
  351.         {
  352.         case 'c':
  353.           /* The next arg is a command to execute, and the following args
  354.          are $1 .. $n respectively. */
  355.         local_pending_command = argv[++arg_index];
  356.         if (!local_pending_command)
  357.           {
  358.             report_error ("`%cc' requires an argument", on_or_off);
  359.             exit (1);
  360.           }
  361.         arg_index++;
  362.         goto after_flags;
  363.  
  364.         case 's':
  365.         read_from_stdin = 1;
  366.         break;
  367.  
  368.         case 'o':
  369.         o_option = argv[next_arg++];
  370.         if (!o_option)
  371.           {
  372.             report_error ("`%co' requires an argument", on_or_off);
  373.             exit (1);
  374.           }
  375.  
  376.         if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
  377.           exit (1);
  378.         break;
  379.  
  380.         default:
  381.           if (change_flag_char (arg_character, on_or_off) == FLAG_ERROR)
  382.         {
  383.           report_error ("%c%c: bad option", on_or_off, arg_character);
  384.           exit (1);
  385.         }
  386.  
  387.         }
  388.     }
  389.       /* Can't do just a simple increment anymore -- what about
  390.      "bash -abouo emacs ignoreeof -hO"? */
  391.       arg_index = next_arg;
  392.     }
  393.  
  394.  after_flags:
  395.  
  396.   /* First, let the outside world know about our interactive status.
  397.      A shell is interactive if the `-i' flag was given, or if all of
  398.      the following conditions are met:
  399.     no -c command
  400.     no arguments remaining or the -s flag given
  401.     standard input is a terminal
  402.     standard output is a terminal
  403.      Refer to Posix.2, the description of the `sh' utility. */
  404.  
  405.   if (forced_interactive ||        /* -i flag */
  406.       (!local_pending_command &&    /* -c command */
  407.        ((arg_index == argc) ||        /* No remaining args or... */
  408.     read_from_stdin) &&        /* -s flag with args */
  409.        isatty (fileno (stdin)) &&    /* Input is a terminal and */
  410.        isatty (fileno (stdout))))    /* output is a terminal. */
  411.     interactive = 1;
  412.   else
  413.     {
  414.       extern int history_expansion;
  415.  
  416.       history_expansion = remember_on_history = 0;
  417.       interactive = 0;
  418. #ifdef JOB_CONTROL
  419.       job_control = 0;
  420. #endif
  421.     }
  422.  
  423. #define CLOSE_FDS_AT_LOGIN
  424.  
  425. #if defined (CLOSE_FDS_AT_LOGIN)
  426.   /*
  427.    * Some systems have the bad habit of starting login shells with lots of open
  428.    * file descriptors.  For instance, most systems that have picked up the
  429.    * pre-4.0 Sun YP code leave a file descriptor open each time you call one
  430.    * of the getpw* functions, and it's set to be open across execs.  That
  431.    * means one for login, one for xterm, one for shelltool, etc.
  432.    */
  433.   if (login_shell && interactive)
  434.     {
  435.       register int i;
  436.       for (i = 3; i < 20; i++)
  437.     close (i);
  438.     }
  439. #endif /* CLOSE_FDS_AT_LOGIN */
  440.  
  441.   /* From here on in, the shell must be a normal functioning shell.
  442.      Variables from the environment are expected to be set, etc. */
  443.   shell_initialize ();
  444.  
  445.   if (interactive)
  446.     {
  447.       char *term = (char *)getenv ("TERM");
  448.       if (term && (strcmp (term, "emacs") == 0))
  449.     no_line_editing = 1;
  450.     }
  451.  
  452.   top_level_arg_index = arg_index;
  453.  
  454.   if (!quiet && do_version)
  455.     show_shell_version ();
  456.  
  457.   /* Give this shell a place to longjmp to before executing the
  458.      startup files.  This allows users to press C-c to abort the
  459.      lengthy startup. */
  460.   if (setjmp (top_level))
  461.     {
  462.       if (!interactive)
  463.     exit (2);
  464.       else
  465.     locally_skip_execution++;
  466.     }
  467.  
  468.   arg_index = top_level_arg_index;
  469.  
  470.   /* Execute the start-up scripts. */
  471.  
  472.   if (!interactive)
  473.     {
  474.       makunbound ("PS1", shell_variables);
  475.       makunbound ("PS2", shell_variables);
  476.       interactive_shell = 0;
  477.     }
  478.   else
  479.     {
  480.       change_flag_char ('i', FLAG_ON);
  481.     }
  482.  
  483.   if (!locally_skip_execution)
  484.     {
  485.       if (login_shell)
  486.     maybe_execute_file ("/etc/profile");
  487.  
  488.       if (login_shell && !no_profile)
  489.     {
  490.       /* If we are doing the .bash_profile, then don't do the .bashrc. */
  491.       no_rc++;
  492.  
  493.       if (act_like_sh)
  494.         maybe_execute_file ("~/.profile");
  495.       else
  496.         {
  497.           if (maybe_execute_file ("~/.bash_profile") == 0)
  498.         if (maybe_execute_file ("~/.bash_login") == 0)
  499.           maybe_execute_file ("~/.profile");
  500.         }
  501.  
  502.     /* I turn on the restrictions afterwards because it is explictly
  503.        stated in the POSIX spec that PATH cannot be set in a restricted
  504.        shell, except in .profile. */
  505.       if (*++(argv[0]) == 'r')
  506.         {
  507.           set_var_read_only ("PATH");
  508.           set_var_read_only ("SHELL");
  509.           restricted++;
  510.         }
  511.     }
  512.  
  513.       /* Execute ~/.bashrc for most shells.  Never execute it if
  514.      ACT_LIKE_SH is set, or if NO_RC is set.
  515.  
  516.      If the executable file "/usr/gnu/src/bash/foo" contains:
  517.  
  518.        #!/usr/gnu/bin/bash
  519.        echo hello
  520.  
  521.      then:
  522.  
  523.      COMMAND        EXECUTE BASHRC
  524.      --------------------------------
  525.      bash -c foo        NO
  526.      bash foo        NO
  527.      foo            NO
  528.      rsh machine ls        YES (for rsh, which calls `bash -c')
  529.      rsh machine foo    YES (for shell started by rsh) NO (for foo!)
  530.      echo ls | bash        NO
  531.      login            YES
  532.      bash            YES
  533.       */
  534.       if (!act_like_sh && !no_rc &&
  535.       (interactive || (!isatty (fileno (stdin)) &&
  536.                local_pending_command)))
  537.     maybe_execute_file (bashrc_file);
  538.  
  539.       /* Try a TMB suggestion.  If running a script, then execute the
  540.      file mentioned in the ENV variable. */
  541.       if (!interactive)
  542.     {
  543.       char *env_file = (char *)getenv ("ENV");
  544.       if (env_file && *env_file)
  545.         {
  546.           WORD_LIST *list, *expand_string_unsplit ();
  547.           char *expanded_file_name, *string_list ();
  548.  
  549.           list = expand_string_unsplit (env_file, 1);
  550.           if (list)
  551.         {
  552.           expanded_file_name = string_list (list);
  553.           dispose_words (list);
  554.           if (expanded_file_name && *expanded_file_name)
  555.             maybe_execute_file (expanded_file_name);
  556.           free (expanded_file_name);
  557.         }
  558.         }
  559.     }
  560.  
  561.       if (local_pending_command)
  562.     {
  563.       /* Bind remaining args to $1 ... $n */
  564.       WORD_LIST *args = (WORD_LIST *)NULL;
  565.       while (arg_index != argc)
  566.         args = make_word_list (make_word (argv[arg_index++]), args);
  567.       args = (WORD_LIST *)reverse_list (args);
  568.       remember_args (args, 1);
  569.       dispose_words (args);
  570.  
  571.       with_input_from_string (local_pending_command, "-c");
  572.       goto read_and_execute;
  573.     }
  574.     }
  575.  
  576.   /* Do the things that should be done only for interactive shells. */
  577.   if (interactive)
  578.     {
  579.       /* Set up for checking for presence of mail. */
  580. #if defined (USG)
  581.       /* Under System V, we can only tell if you have mail if the
  582.      modification date has changed.  So remember the current
  583.      modification dates. */
  584.       remember_mail_dates ();
  585. #else
  586.       /* Under 4.x, you have mail if there is something in your inbox.
  587.      I set the remembered mail dates to 1900.  */
  588.       reset_mail_files ();
  589. #endif /* USG */
  590.  
  591.       /* If this was a login shell, then assume that /bin/login has already
  592.      taken care of informing the user that they have new mail.  Otherwise,
  593.      we want to check right away. */
  594.       if (login_shell == 1)
  595.     {
  596. #if !defined (USG)
  597.       remember_mail_dates ();
  598. #endif
  599.     }
  600.  
  601.       reset_mail_timer ();
  602.  
  603.       change_flag_char ('i', FLAG_ON);
  604.       
  605.       /* Initialize the interactive history stuff. */
  606.       if (!shell_initialized)
  607.     {
  608.       char *hf;
  609.  
  610.       /* Truncate history file for interactive shells which desire it.
  611.          Note that the history file is automatically truncated to the
  612.          size of HISTSIZE if the user does not explicitly set the size
  613.          differently. */
  614.       set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
  615.       stupidly_hack_special_variables ("HISTFILESIZE");
  616.  
  617.       /* Read the history in HISTFILE into the history list. */
  618.       hf = get_string_value ("HISTFILE");
  619.  
  620.       if (hf)
  621.         {
  622.           struct stat buf;
  623.  
  624.           if (stat (hf, &buf) == 0)
  625.         {
  626.           void using_history ();
  627.  
  628.           read_history (hf);
  629.           using_history ();
  630.           history_lines_in_file = where_history ();
  631.         }
  632.         }
  633.     }
  634.     }
  635.  
  636.   /* Get possible input filename. */
  637.   if ((arg_index != argc) && !read_from_stdin)
  638.     {
  639.       int fd;
  640.       char *filename;
  641.       extern char *find_path_file ();
  642.       extern int errno;
  643.  
  644.       free (dollar_vars[0]);
  645.       dollar_vars[0] = savestring (argv[arg_index]);
  646.       filename = savestring (argv[arg_index]);
  647.  
  648.       fd = open (filename, O_RDONLY);
  649.       if ((fd < 0) && (errno == ENOENT))
  650.     {
  651.       char *path_filename;
  652.       /* If it's not in the current directory, try looking through PATH
  653.          for it. */
  654.       path_filename = find_path_file (argv[arg_index]);
  655.       if (path_filename)
  656.         {
  657.           free (filename);
  658.           filename = path_filename;
  659.           fd = open (filename, O_RDONLY);
  660.         }
  661.     }
  662.  
  663.       arg_index++;
  664.       if (fd < 0)
  665.     {
  666.       file_error (filename);
  667.       exit (1);
  668.     }
  669.  
  670.       /* Only do this with file descriptors we can seek on. */
  671.       if (lseek (fd, 0L, 1) != -1)
  672.     {
  673.       unsigned char sample[80];
  674.       int sample_len;
  675.  
  676.       /* Check to see if the `file' in `bash file' is a binary file
  677.          according to the same tests done by execute_simple_command (),
  678.          and report an error and exit if it is. */
  679.       sample_len = read (fd, sample, sizeof (sample));
  680.       if (sample_len > 0)
  681.         if (check_binary_file (sample, sample_len))
  682.           {
  683.         report_error ("%s: cannot execute binary file", filename);
  684.         exit (EX_BINARY_FILE);
  685.           }
  686.       /* Now rewind the file back to the beginning. */
  687.       lseek (fd, 0L, 0);
  688.     }
  689.  
  690.       default_input = fdopen (fd, "r");
  691.  
  692.       if (!default_input)
  693.     {
  694.       file_error (filename);
  695.       exit (127);
  696.     }
  697.  
  698.       SET_CLOSE_ON_EXEC (fd);
  699.       if (fileno (default_input) != fd)
  700.     SET_CLOSE_ON_EXEC (fileno (default_input));
  701.  
  702.       if (!interactive || (!isatty (fd)))
  703.     {
  704.       extern int history_expansion;
  705.       history_expansion = interactive = remember_on_history = 0;
  706. #ifdef JOB_CONTROL
  707.       set_job_control (0);
  708. #endif
  709.     }
  710.       else
  711.     {
  712.       /* I don't believe that this code is ever executed, even in
  713.          the presence of /dev/fd. */
  714.       dup2 (fd, 0);
  715.       close (fd);
  716.       fclose (default_input);
  717.     }
  718.     }
  719.  
  720.   /* Bind remaining args to $1 ... $n */
  721.   {
  722.     WORD_LIST *args = (WORD_LIST *)NULL;
  723.     while (arg_index != argc)
  724.       args = make_word_list (make_word (argv[arg_index++]), args);
  725.     args = (WORD_LIST *)reverse_list (args);
  726.     remember_args (args, 1);
  727.     dispose_words (args);
  728.   }
  729.  
  730.   unset_nodelay_mode (fileno (stdin));
  731.  
  732.   /* with_input_from_stdin really means `with_input_from_readline' */
  733.   if (interactive && !no_line_editing)
  734.     with_input_from_stdin ();
  735.   else
  736.     with_input_from_stream (default_input, dollar_vars[0]);
  737.  
  738.  read_and_execute:
  739.  
  740.   shell_initialized = 1;
  741.  
  742.   /* Read commands until exit condition. */
  743.   reader_loop ();
  744.  
  745.   /* Do trap[0] if defined. */
  746.   run_exit_trap ();
  747.  
  748.   maybe_save_shell_history ();
  749.  
  750. #if defined (JOB_CONTROL)
  751.   if (interactive && job_control)
  752.     give_terminal_to (original_pgrp);
  753. #endif /* JOB_CONTROL */
  754.  
  755.   /* Always return the exit status of the last command to our parent. */
  756.   exit (last_command_exit_value);
  757. }
  758.  
  759. /* If this is an interactive shell, then append the lines executed
  760.    this session to the history file. */
  761. int
  762. maybe_save_shell_history ()
  763. {
  764.   int result = 0;
  765.  
  766.   if (interactive && history_lines_this_session)
  767.     {
  768.       void using_history ();
  769.       char *hf = get_string_value ("HISTFILE");
  770.  
  771.       if (hf)
  772.     {
  773.       struct stat buf;
  774.  
  775.       /* If the file doesn't exist, then create it. */
  776.       if (stat (hf, &buf) == -1)
  777.         {
  778.           int file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
  779.           if (file != -1)
  780.         close (file);
  781.         }
  782.  
  783.       /* Now actually append the lines if the history hasn't been
  784.          stifled. */
  785.       using_history ();
  786.       if (history_lines_this_session <= where_history ())
  787.         {
  788.           result = append_history (history_lines_this_session, hf);
  789.           history_lines_in_file += history_lines_this_session;
  790.           history_lines_this_session = 0;
  791.         }
  792.     }
  793.     }
  794.   return (result);
  795. }
  796.  
  797. /* Try to execute the contents of FNAME.  If FNAME doesn't exist,
  798.    that is not an error, but other kinds of errors are.  Returns
  799.    -1 in the case of an error, 0 in the case that the file was not
  800.    found, and 1 if the file was found and executed. */
  801. maybe_execute_file (fname)
  802.      char *fname;
  803. {
  804.   extern int errno;
  805.   char *tilde_expand ();
  806.   char *filename = tilde_expand (fname);
  807.  
  808.   struct stat file_info;
  809.  
  810.   int tt, tresult;
  811.   char *string;
  812.   int fd = open (filename, O_RDONLY);
  813.  
  814.   if (fd < 0)
  815.     {
  816. file_error_and_exit:
  817.       if (errno != ENOENT)
  818.     file_error (filename);
  819.       free (filename);
  820.       return ((errno == ENOENT) ? 0 : -1);
  821.     }
  822.  
  823.   if (fstat (fd, &file_info) == -1)
  824.     goto file_error_and_exit;
  825.  
  826.   string = (char *)xmalloc (1 + file_info.st_size);
  827.   tresult = read (fd, string, file_info.st_size);
  828.   tt = errno;
  829.   close (fd);
  830.   errno = tt;
  831.   if (tresult != file_info.st_size)
  832.     {
  833.       free (string);
  834.       goto file_error_and_exit;
  835.     }
  836.   string[file_info.st_size] = '\0';
  837.  
  838.   parse_and_execute (string, filename);
  839.   free (filename);
  840.  
  841.   return (1);
  842. }
  843.  
  844. reader_loop ()
  845. {
  846.   extern int indirection_level;
  847.   int our_indirection_level;
  848.   COMMAND *current_command = (COMMAND *)NULL;
  849.  
  850.   our_indirection_level = ++indirection_level;
  851.  
  852.   while (!EOF_Reached)
  853.     {
  854.       sighandler sigint_sighandler ();
  855.       int code = setjmp (top_level);
  856.       extern char *trap_list[];
  857.  
  858.       signal (SIGINT, sigint_sighandler);
  859.  
  860.       if (code != NOT_JUMPED)
  861.     {
  862.       indirection_level = our_indirection_level;
  863.  
  864.       switch (code)
  865.         {
  866.           /* Some kind of throw to top_level has occured. */
  867.         case FORCE_EOF:
  868.         case EXITPROG:
  869.           current_command = (COMMAND *)NULL;
  870.           EOF_Reached = EOF;
  871.           goto exec_done;
  872.  
  873.         case DISCARD:
  874.           /* Obstack free command elements, etc. */
  875.           break;
  876.  
  877.         default:
  878.           programming_error ("Bad jump %d", code);
  879.         }
  880.     }
  881.  
  882.       executing = 0;
  883.       dispose_used_env_vars ();
  884.  
  885.       if (read_command () == 0)
  886.     {
  887.       if (global_command) {
  888.         current_command = global_command;
  889.  
  890.         current_command_number++;
  891.  
  892.         /* POSIX spec: "-n    The shell reads commands but does
  893.            not execute them; this can be used to check for shell
  894.            script syntax errors.  The shell ignores the -n option
  895.            for interactive shells. " */
  896.  
  897.         if (interactive || !read_but_dont_execute)
  898.           {
  899.         executing = 1;
  900.         execute_command (current_command);
  901.           }
  902.  
  903.       exec_done:
  904.         if (current_command)
  905.           dispose_command (current_command);
  906.         QUIT;
  907.       }
  908.     }
  909.       else
  910.     {
  911.       /* Parse error, maybe discard rest of stream if not interactive. */
  912.       if (!interactive)
  913.         EOF_Reached = EOF;
  914.     }
  915.       if (just_one_command)
  916.     EOF_Reached = EOF;
  917.     }
  918.   indirection_level--;
  919. }
  920.  
  921. /* Return a string denoting what our indirection level is. */
  922. static char indirection_string[100];
  923.  
  924. char *
  925. indirection_level_string ()
  926. {
  927.   register int i, j;
  928.   char *get_string_value (), *ps4 = get_string_value ("PS4");
  929.   extern char *decode_prompt_string ();
  930.  
  931.   if (!ps4)
  932.     ps4 = savestring ("+ ");
  933.   else
  934.     ps4 = decode_prompt_string (ps4);
  935.  
  936.   for (i = 0; i < indirection_level && i < 99; i++)
  937.     indirection_string[i] = *ps4;
  938.  
  939.   for (j = 1; ps4[j] && i < 99; i++, j++)
  940.     indirection_string[i] = ps4[j];
  941.  
  942.   indirection_string[i] = '\0';
  943.   free (ps4);
  944.   return (indirection_string);
  945. }
  946.  
  947. static sighandler 
  948. alrm_catcher(i)
  949.      int i;
  950. {
  951.   printf ("%ctimed out waiting for input: auto-logout\n", '\07');
  952.   longjmp (top_level, EXITPROG);
  953. #ifdef ibm032
  954.   return (0);    /* To shut up the compiler */
  955. #endif
  956. }
  957.  
  958. parse_command ()
  959. {
  960.   extern int need_here_doc;
  961.   extern REDIRECT *redirection_needing_here_doc;
  962.   int r;
  963.   
  964.   need_here_doc = 0;
  965.   redirection_needing_here_doc = (REDIRECT *)NULL;
  966.  
  967.   run_pending_traps ();
  968.  
  969.   r = yyparse ();
  970.  
  971.   if (need_here_doc)
  972.     make_here_document (redirection_needing_here_doc);
  973.   need_here_doc = 0;
  974.  
  975.   return r;
  976. }
  977.  
  978. read_command ()
  979. {
  980.   extern char *ps1_prompt, **prompt_string_pointer;
  981.   SHELL_VAR *tmout_var = (SHELL_VAR *)NULL;
  982.   int tmout_len = 0, result;
  983.   SigHandler *old_alrm;
  984.  
  985.   prompt_string_pointer = &ps1_prompt;
  986.   global_command = (COMMAND *)NULL;
  987.  
  988.   /* Only do timeouts if interactive. */
  989.   if (interactive)
  990.     {
  991.       tmout_var = find_variable ("TMOUT");
  992.  
  993.       if (tmout_var && tmout_var->value)
  994.     {
  995.       tmout_len = atoi (tmout_var->value);
  996.       if (tmout_len > 0)
  997.         {
  998.           old_alrm = signal (SIGALRM, alrm_catcher);
  999.           alarm (tmout_len);
  1000.         }
  1001.     }
  1002.     }
  1003.  
  1004.   QUIT;
  1005.  
  1006.   result = parse_command ();
  1007.  
  1008.   if (interactive && tmout_var && (tmout_len > 0))
  1009.     {
  1010.       alarm(0);
  1011.       signal (SIGALRM, old_alrm);
  1012.     }
  1013.   return (result);
  1014. }
  1015.  
  1016. /* Do whatever is necessary to initialize the shell.
  1017.    Put new initializations in here. */
  1018. shell_initialize ()
  1019. {
  1020.   /* Line buffer output for stderr.
  1021.      If your machine doesn't have either of setlinebuf or setvbuf,
  1022.      you can just comment out the buffering commands, and the shell
  1023.      will still work.  It will take more cycles, though. */
  1024. #if defined (HAVE_SETLINEBUF)
  1025.   setlinebuf (stderr);
  1026.   setlinebuf (stdout);
  1027. #else
  1028. # if defined (_IOLBF)
  1029.   setvbuf (stderr, (char *)NULL, _IOLBF, BUFSIZ);
  1030.   setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
  1031. # endif
  1032. #endif /* HAVE_SETLINEBUF */
  1033.  
  1034.   /* Initialize the trap signal handlers before installing our own
  1035.      signal handlers.  traps.c:restore_default_signal () is responsible
  1036.      for restoring the original default signal handler.  That function
  1037.      is called from jobs.c when we make a new child. */
  1038.   initialize_traps ();
  1039.   initialize_signals ();
  1040.  
  1041.   /* Initialize current_user_name and current_host_name. */
  1042.   {
  1043.     struct passwd *entry = getpwuid (getuid ());
  1044.     char hostname[256];
  1045.  
  1046.     if (gethostname (hostname, 255) < 0)
  1047.       current_host_name = "??host??";
  1048.     else
  1049.       current_host_name = savestring (hostname);
  1050.  
  1051.     if (entry)
  1052.       current_user_name = savestring (entry->pw_name);
  1053.     else
  1054.       current_user_name = savestring ("I have no name!");
  1055.     endpwent ();
  1056.   }
  1057.  
  1058.   initialize_shell_variables (shell_environment);
  1059.   initialize_filename_hashing ();
  1060.   initialize_jobs ();
  1061. }
  1062.  
  1063. /* Function called by main () when it appears that the shell has already
  1064.    had some initialization preformed.  This is supposed to reset the world
  1065.    back to a pristine state, as if we had been exec'ed. */
  1066. shell_reinitialize ()
  1067. {
  1068.   extern int line_number, last_command_exit_value;
  1069.  
  1070.   /* The default shell prompts. */
  1071.   primary_prompt = PPROMPT;
  1072.   secondary_prompt = SPROMPT;
  1073.  
  1074.   /* Things that get 1. */
  1075.   current_command_number = 1;
  1076.  
  1077.   /* We have decided that the ~/.bashrc file should not be executed
  1078.      for the invocation of each shell script.  Perhaps some other file
  1079.      should.  */
  1080.   act_like_sh = 1;
  1081.  
  1082.   /* Things that get 0. */
  1083.   login_shell = make_login_shell = interactive = restricted = executing = 0;
  1084.   debugging = no_rc = no_profile = do_version = line_number = 0;
  1085.   last_command_exit_value = remember_on_history = 0;
  1086.   forced_interactive = interactive_shell = 0;
  1087.  
  1088.   /* Ensure that the default startup file is used.  (Except that we don't
  1089.      execute this file for reinitialized shells). */
  1090.   bashrc_file = "~/.bashrc";
  1091.  
  1092.   /* Delete all variables and functions.  They will be reinitialized when
  1093.      the environment is parsed. */
  1094.  
  1095.   delete_all_variables (shell_variables);
  1096.   delete_all_variables (shell_functions);
  1097.   
  1098.   /* Pretend the PATH variable has changed. */
  1099.   sv_path ("PATH");
  1100. }
  1101.  
  1102. initialize_signals ()
  1103. {
  1104.   initialize_terminating_signals ();
  1105.   initialize_job_signals ();
  1106. #if defined (INITIALIZE_SIGLIST)
  1107.   initialize_siglist ();
  1108. #endif
  1109. }
  1110.  
  1111. /* The list of signals that would terminate the shell if not caught.
  1112.    We catch them, but just so that we can write the history file,
  1113.    and so forth. */
  1114. int terminating_signals[] = {
  1115. #ifdef SIGHUP
  1116.   SIGHUP,
  1117. #endif
  1118.  
  1119. #ifdef SIGINT
  1120.   SIGINT,
  1121. #endif
  1122.  
  1123. #ifdef SIGQUIT
  1124.   SIGQUIT,
  1125. #endif
  1126.  
  1127. #ifdef SIGILL
  1128.   SIGILL,
  1129. #endif
  1130.  
  1131. #ifdef SIGTRAP
  1132.   SIGTRAP,
  1133. #endif
  1134.  
  1135. #ifdef SIGIOT
  1136.   SIGIOT,
  1137. #endif
  1138.  
  1139. #ifdef SIGDANGER
  1140.   SIGDANGER,
  1141. #endif
  1142.  
  1143. #ifdef SIGEMT
  1144.   SIGEMT,
  1145. #endif
  1146.  
  1147. #ifdef SIGFPE
  1148.   SIGFPE,
  1149. #endif
  1150.  
  1151.  
  1152. #ifdef SIGKILL
  1153.   SIGKILL,
  1154. #endif
  1155.  
  1156.  
  1157. #ifdef SIGBUS
  1158.   SIGBUS,
  1159. #endif
  1160.  
  1161.  
  1162. #ifdef SIGSEGV
  1163.   SIGSEGV,
  1164. #endif
  1165.  
  1166. #ifdef SIGSYS
  1167.   SIGSYS,
  1168. #endif
  1169.  
  1170. #ifdef SIGPIPE
  1171.   SIGPIPE,
  1172. #endif
  1173.  
  1174. #ifdef SIGALRM
  1175.   SIGALRM,
  1176. #endif
  1177.  
  1178. #ifdef SIGTERM
  1179.   SIGTERM,
  1180. #endif
  1181.  
  1182. #ifdef SIGXCPU
  1183.   SIGXCPU,
  1184. #endif
  1185. #ifdef SIGXFSZ
  1186.   SIGXFSZ,
  1187. #endif
  1188. #ifdef SIGVTALRM
  1189.   SIGVTALRM,
  1190. #endif
  1191. #ifdef SIGPROF
  1192.   SIGPROF,
  1193. #endif
  1194. #ifdef SIGLOST
  1195.   SIGLOST,
  1196. #endif
  1197. #ifdef SIGUSR1
  1198.   SIGUSR1, SIGUSR2
  1199. #endif
  1200.     };
  1201.  
  1202. #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
  1203.  
  1204. /* This function belongs here? */
  1205. sighandler
  1206. termination_unwind_protect (sig)
  1207.      int sig;
  1208. {
  1209.   maybe_save_shell_history ();
  1210.   signal (sig, SIG_DFL);
  1211.   kill (getpid (), sig);
  1212. #ifdef ibm032
  1213.   return (0);    /* To shut up the compiler */
  1214. #endif
  1215. }
  1216.  
  1217. /* Initialize signals that will terminate the shell to do some
  1218.    unwind protection. */
  1219. initialize_terminating_signals ()
  1220. {
  1221.   register int i;
  1222.  
  1223. #if defined (_POSIX_VERSION)
  1224.   /* If we're running on a Posix-compliant system, do things the Posix way. */
  1225.  
  1226.   struct sigaction act;
  1227.  
  1228.   act.sa_handler = termination_unwind_protect;
  1229.   act.sa_flags = 0;
  1230.   sigemptyset (&act.sa_mask);
  1231.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1232.     sigaddset (&act.sa_mask, terminating_signals[i]);
  1233.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1234.     sigaction (terminating_signals[i], &act, (struct sigaction *)NULL);
  1235.  
  1236.   /* Clear signal mask for interactive login shell. */
  1237.   if (login_shell)
  1238.     {
  1239.       sigset_t set;
  1240.  
  1241.       sigemptyset (&set);
  1242.       sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  1243.     }
  1244.  
  1245. #else /* _POSIX_VERSION */
  1246.  
  1247.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1248.     signal (terminating_signals[i], termination_unwind_protect);
  1249.  
  1250. #endif /* _POSIX_VERSION */
  1251.  
  1252.   /* And, some signals that are specifically ignored by the shell. */
  1253.   signal (SIGQUIT, SIG_IGN);
  1254.  
  1255.   if (login_shell)
  1256.     signal (SIGTERM, SIG_IGN);
  1257. }
  1258.  
  1259. /* What to do when we've been interrupted, and it is safe to handle it. */
  1260. sighandler
  1261. throw_to_top_level ()
  1262. {
  1263.   extern int last_command_exit_value, loop_level, continuing, breaking;
  1264.   extern int return_catch_flag;
  1265.   extern int special_source_interrupt; 
  1266.   int print_newline = 0;
  1267.  
  1268.   if (special_source_interrupt)
  1269.     {
  1270.       extern jmp_buf special_buf;
  1271.  
  1272.       longjmp (special_buf, 1);
  1273.     }
  1274.  
  1275.   if (interrupt_state)
  1276.     {
  1277.       print_newline = 1;
  1278.       interrupt_state--;
  1279.     }
  1280.  
  1281.   if (interrupt_state)
  1282.     return;
  1283.  
  1284. #if defined (JOB_CONTROL)
  1285.   give_terminal_to (shell_pgrp);
  1286. #endif /* JOB_CONTROL */
  1287.  
  1288.   run_unwind_protects ();
  1289.   loop_level = continuing = breaking = 0;
  1290.   return_catch_flag = 0;
  1291.  
  1292.   reset_parser ();
  1293.  
  1294.   if (interactive && print_newline)
  1295.     {
  1296.       fflush (stdout);
  1297.       fprintf (stderr, "\n");
  1298.     }
  1299.  
  1300. #if defined (READLINE)
  1301.   if (interactive)
  1302.     bashline_reinitialize ();
  1303. #endif /* READLINE */
  1304.  
  1305.   /* Run any traps set on interrupt. */
  1306.   run_interrupt_trap ();
  1307.  
  1308.   last_command_exit_value |= 128;
  1309.  
  1310.   if (interactive)
  1311.     longjmp (top_level, DISCARD);
  1312.   else
  1313.     longjmp (top_level, EXITPROG);
  1314. }
  1315.  
  1316. /* When non-zero, we throw_to_top_level (). */
  1317. int interrupt_immediately = 0;
  1318.  
  1319. /* What we really do when SIGINT occurs. */
  1320. sighandler
  1321. sigint_sighandler (sig)
  1322.      int sig;
  1323. {
  1324. #if defined (USG) && !defined (_POSIX_VERSION)
  1325.   signal (sig, sigint_sighandler);
  1326. #endif
  1327.  
  1328.   /* interrupt_state needs to be set for the stack of interrupts to work
  1329.      right.  Should it be set unconditionally? */
  1330.   if (!interrupt_state)
  1331.     interrupt_state++;
  1332.   if (interrupt_immediately)
  1333.     {
  1334.       interrupt_immediately = 0;
  1335.       throw_to_top_level ();
  1336.     }
  1337. #ifdef ibm032
  1338.   return (0);    /* To shut up the compiler */
  1339. #endif
  1340. }
  1341.     
  1342. /* Write the existing history out to the history file. */
  1343. save_history ()
  1344. {
  1345.   void using_history ();
  1346.  
  1347.   using_history ();
  1348.   write_history (get_string_value ("HISTFILE"));
  1349. }
  1350.  
  1351. #if defined (MAKE_BUG_REPORTS)
  1352. /* Make a bug report, even to the extent of mailing it.  Hope that it
  1353.    gets where it is supposed to go.  If not, maybe the user will send
  1354.    it back to me. */
  1355. #include <readline/history.h>
  1356. /* Number of commands to place in the bug report. */
  1357. #define LAST_INTERESTING_HISTORY_COUNT 6
  1358.  
  1359. #if defined (HAVE_VPRINTF)
  1360. make_bug_report (va_alist)
  1361.      va_dcl
  1362. #else
  1363. make_bug_report (reason, arg1, arg2)
  1364.      char *reason;
  1365. #endif /* HAVE_VPRINTF */
  1366. {
  1367.   extern char *current_host_name, *current_user_name, *the_current_maintainer;
  1368.   extern int interactive, login_shell;
  1369.   register int len = where_history ();
  1370.   register int i = len - LAST_INTERESTING_HISTORY_COUNT;
  1371.   FILE *stream, *popen ();
  1372.   HIST_ENTRY **list = history_list ();
  1373.  
  1374. #if defined (HAVE_VPRINTF)
  1375.   char *reason;
  1376.   va_list args;
  1377. #endif /* HAVE_VPRINTF */
  1378.  
  1379.   stream = popen ("/bin/rmail bash-maintainers@ai.mit.edu", "w");
  1380.  
  1381.   save_history ();
  1382.   if (i < 0) i = 0;
  1383.  
  1384.   if (stream)
  1385.     {
  1386.       fprintf (stream, "To: bash-maintainers@ai.mit.edu\n");
  1387.       fprintf (stream, "Subject: Bash-%s.%d bug-report: ",
  1388.            dist_version, build_version);
  1389.  
  1390. #if defined (HAVE_VPRINTF)
  1391.       va_start (args);
  1392.       reason = va_arg (args, char *);
  1393.       vfprintf (stream, reason, args);
  1394.       va_end (args);
  1395. #else
  1396.       fprintf (stream, reason, arg1, arg2);
  1397. #endif                /* HAVE_VPRINTF */
  1398.  
  1399.       fprintf (stream, "\n");
  1400.  
  1401.       /* Write the history into the mail file.  Maybe we can recreate
  1402.      the bug? */
  1403.       fprintf (stream,
  1404.            "This is a Bash bug report.  Bash maintainers should be getting this report.\n\
  1405. If this mail has bounced, for right now please send it to:\n\
  1406. \n\
  1407.     %s\n\
  1408. \n\
  1409. since he is the current maintainer of this version of the shell.\n\
  1410. \n\
  1411. This is %s (invoked as `%s'), version %s.%d, on host %s, used by %s.\n\
  1412. This shell is %sinteractive, and it is %sa login shell.\n\
  1413. \n\
  1414. The host is a %s running %s.\n\
  1415. \n\
  1416. The current environment is:\n",
  1417.            the_current_maintainer,
  1418.            get_string_value ("BASH"), full_pathname (dollar_vars[0]),
  1419.            dist_version, build_version, current_host_name,
  1420.            current_user_name, interactive ? "" : "not ",
  1421.            login_shell ? "" : "not ", SYSTEM_NAME, OS_NAME);
  1422.  
  1423.       {
  1424.     SHELL_VAR **vlist, *var;
  1425.     register int i;
  1426.  
  1427.     vlist = all_shell_variables ();
  1428.  
  1429.     for (i = 0; vlist && var = vlist[i]; i++)
  1430.       {
  1431.         if (!invisible_p (var) && exported_p (var))
  1432.           {
  1433.         fprintf (stream, "%s=%s", var->name, value_cell (var));
  1434.         fprintf (stream, "\n");
  1435.           }
  1436.       }
  1437.       }
  1438.  
  1439.       fprintf (stream, "\nAnd here are the last %d commands.\n\n",
  1440.            LAST_INTERESTING_HISTORY_COUNT);
  1441.  
  1442.       for (; i < len; i++)
  1443.     fprintf (stream, "%s\n", list[i]->line);
  1444.  
  1445.       pclose (stream);
  1446.     }
  1447.   else
  1448.     {
  1449.       fprintf (stderr, "Can't mail bug report!\n");
  1450.     }
  1451. }
  1452. #endif /* MAKE_BUG_REPORTS */
  1453.  
  1454. /* Give version information about this shell. */
  1455. show_shell_version ()
  1456. {
  1457.   extern char *shell_name;
  1458.   extern int version;
  1459.  
  1460.   printf ("GNU %s, version %s.%d\n", base_pathname (shell_name),
  1461.       dist_version, build_version);
  1462. }
  1463.